﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

#include <QCoreApplication>
#include <qnetworktrafficmonitor.h>
#include <QDebug>
#include <QThread>
#include <qnetworkadapterinfoprovider.h>
#include <qmotherboardinfoprovider.h>
#include <qgpuinfoprovider.h>

void motherboardInfo(){
    QMotherboardInfoProvider provider;
    QList<QMotherboardInfoProvider::MotherboardDetails> details = provider.getAllMotherboardDetails();

    for (const auto &detail : details) {
        qDebug() << "Manufacturer:" << detail.manufacturer;
        qDebug() << "Product:" << detail.product;
        qDebug() << "Serial Number:" << detail.serialNumber;
        qDebug() << "Version:" << detail.version;
        qDebug() << "Caption:" << detail.caption;
        qDebug() << "-----------------------------";
    }
}

void networkAdapterInfo(){
    // 创建网络适配器信息提供者实例
    QNetworkAdapterInfoProvider provider;

    auto adapters = provider.getNetworkAdapters();

    qDebug() << "检测到" << adapters.size() << "个网络适配器：\n";
    for (const auto& adapter : adapters) {
        qDebug() << "=== " << adapter.name << " ===";
        qDebug() << "描述：" << adapter.description;
        qDebug() << "MAC地址：" << adapter.macAddress;
        qDebug() << "索引：" << adapter.index;
        qDebug() << "MTU：" << adapter.mtu;
        qDebug() << "速度：" << adapter.speed / 1000000 << "Mbps";
        qDebug() << "管理状态：" << (adapter.isAdminUp ? "启用" : "禁用");
        qDebug() << "操作状态：" << adapter.operationalStatus;
        qDebug() << "接口类型：" << adapter.type << "\n";
    }
}

// 辅助函数：格式化字节数显示
QString formatBytes(quint64 bytes) {
    if (bytes < 1024) return QString("%1 B").arg(bytes);
    if (bytes < 1024 * 1024) return QString("%1 KB").arg(bytes / 1024.0, 0, 'f', 1);
    if (bytes < 1024 * 1024 * 1024) return QString("%1 MB").arg(bytes / (1024.0 * 1024), 0, 'f', 1);
    return QString("%1 GB").arg(bytes / (1024.0 * 1024 * 1024), 0, 'f', 1);
}

// 格式化速率显示
QString formatSpeed(double speed) {
    if (speed < 1024) return QString("%1 B/s").arg(speed, 0, 'f', 1);
    if (speed < 1024 * 1024) return QString("%1 KB/s").arg(speed / 1024.0, 0, 'f', 1);
    return QString("%1 MB/s").arg(speed / (1024.0 * 1024), 0, 'f', 1);
}


void networkTrafficMonitor(){
    QNetworkTrafficMonitor monitor;
    // 显示各接口详细信息
    auto interfaces = monitor.getInterfaceTraffics();
    foreach (const auto& iface, interfaces) {
        qDebug() << "接口:" << iface.name;
        qDebug() << "  描述:" << iface.description;
        qDebug() << "  MAC地址:" << iface.macAddress;
        qDebug() << "  状态:" << (iface.isUp ? "启用" : "禁用");
        qDebug() << "  总接收:" << formatBytes(iface.bytesReceived);
        qDebug() << "  总发送:" << formatBytes(iface.bytesSent);
        qDebug() << "  接收速率:" << formatSpeed(iface.downloadRate);
        qDebug() << "  发送速率:" << formatSpeed(iface.uploadRate);
        qDebug() << "------------------------";
    }
        for(int i = 0; i < 1000;i++){

            // 显示总流量信息
            auto total = monitor.getTotalTraffic();
            qDebug() << "=== 总流量 ===";
            qDebug() << "总接收:" << formatBytes(total.totalBytesReceived);
            qDebug() << "总发送:" << formatBytes(total.totalBytesSent);
            qDebug() << "总接收速率:" << formatSpeed(total.totalDownloadRate);
            qDebug() << "总发送速率:" << formatSpeed(total.totalUploadRate);
            qDebug() << "\n====================================\n";
            QThread::msleep(1000);
        }
}

void gpuInfo(){
    QGpuInfoProvider &provider = QGpuInfoProvider::getInstance();

    qDebug() << "GPU Count:" << provider.getDeviceCount();
    qDebug() << "Average GPU Usage:" << provider.usage() << "%";

    auto gpus = provider.getAllGpuDetails();
    for (const auto &gpu : gpus) {
        qDebug() << "GPU Name:" << gpu.name;
        qDebug() << "PCI Bus ID:" << gpu.pciBusId;
        qDebug() << "Temperature:" << gpu.temperature << "°C";
        qDebug() << "Memory Used:" << gpu.memoryUsed / 1024 / 1024 << "MB /" << gpu.memoryTotal / 1024 / 1024 << "MB";
        qDebug() << "---------------------------------------------";
    }

    QGpuInfoProvider::deleteInstance();
}

void gpuMonitor(){
    QGpuInfoProvider &provider = QGpuInfoProvider::getInstance();
    for(int i = 0; i < 10;i++){
        qDebug() << "Memory Used:" << provider.usage();
        QThread::msleep(1000);
    }
    QGpuInfoProvider::deleteInstance();
}

int main(int argc, char *argv[])
{

    QCoreApplication a(argc, argv);
    //    motherboardInfo();
    networkAdapterInfo();

    networkTrafficMonitor();
    //    gpuInfo();
    //    gpuMonitor();

    system("pause");
    return a.exec();
}
